package com.openroom.ui.component;

import org.apache.pivot.wtk.Bounds;
import org.apache.pivot.wtk.Component;
import org.apache.pivot.wtk.Dimensions;
import org.apache.pivot.wtk.Panorama;
import org.apache.pivot.wtk.Viewport;
import org.apache.pivot.wtk.ViewportListener;
import org.apache.pivot.wtk.media.Image;
import org.apache.pivot.wtk.skin.ContainerSkin;

import com.openroom.ui.PreviewImageView;

public class FullViewImageContainerSkin extends ContainerSkin implements
        Viewport.Skin, ViewportListener {

    private float xRatio;
    private float yRatio;

    @Override
    public void install(Component component) {
        super.install(component);
        Panorama panorama = (Panorama) component;
        panorama.getViewportListeners().add(this);
    }

    @Override
    public void layout() {
        Panorama panorama = (Panorama) getComponent();
        int width = getWidth();
        int height = getHeight();

        Component view = panorama.getView();
        if (view != null) {
            Dimensions viewSize = view.getPreferredSize();
            view.setSize(Math.max(width, viewSize.width),
                    Math.max(height, viewSize.height));
            int viewWidth = view.getWidth();
            int viewHeight = view.getHeight();

            int maxScrollTop = getMaxScrollTop();
            if (panorama.getScrollTop() > maxScrollTop) {
                panorama.setScrollTop(maxScrollTop);
            }

            int maxScrollLeft = getMaxScrollLeft();
            if (panorama.getScrollLeft() > maxScrollLeft) {
                panorama.setScrollLeft(maxScrollLeft);
            }

        }

        Image image = ((FullViewImage) panorama.getView()).getImage();
        xRatio = image.getWidth() / getWidth();
        yRatio = image.getHeight() / getHeight();
    }

    protected int getMaxScrollTop() {
        int maxScrollTop = 0;

        Panorama panorama = (Panorama) getComponent();
        int height = getHeight();

        Component view = panorama.getView();
        if (view != null) {
            maxScrollTop = Math.max(view.getHeight() - height, 0);
        }

        return maxScrollTop;
    }

    protected int getMaxScrollLeft() {
        int maxScrollLeft = 0;

        Panorama panorama = (Panorama) getComponent();
        int width = getWidth();

        Component view = panorama.getView();
        if (view != null) {
            maxScrollLeft = Math.max(view.getWidth() - width, 0);
        }
        return maxScrollLeft;
    }

    @Override
    public void scrollTopChanged(Viewport panorama, int previousScrollTop) {
        Component view = panorama.getView();
        if (view != null) {
            int maxScrollTop = getMaxScrollTop();
            int scrollTop = Math.min(panorama.getScrollTop(), maxScrollTop);
            view.setLocation(view.getX(), -scrollTop);
        }
    }

    @Override
    public void scrollLeftChanged(Viewport panorama, int previousScrollLeft) {
        Component view = panorama.getView();
        if (view != null) {
            int maxScrollLeft = getMaxScrollLeft();
            int scrollLeft = Math.min(panorama.getScrollLeft(), maxScrollLeft);
            view.setLocation(-scrollLeft, view.getY());
        }
    }

    @Override
    public void viewChanged(Viewport panorama, Component previousView) {
        invalidateComponent();
    }

    @Override
    public Bounds getViewportBounds() {
        int x = 0;
        int y = 0;
        int width = getWidth();
        int height = getHeight();

        return new Bounds(x, y, width, height);
    }
}
